//
//  CustomTabView.m
//  CustomButton
//
//  Created by 来定MAC on 15/7/21.
//  Copyright (c) 2015年 LD. All rights reserved.
//

#import "CustomTabView.h"
#import "CustomTabButton.h"

@interface CustomTabView ()<CustomTabButtonDelegate>

@property(nonatomic,strong)CustomTabButton *homeTabBtn;

@property(nonatomic,strong)CustomTabButton *buyTabBtn;

@property(nonatomic,strong)CustomTabButton *mineTabBtn;

@property(nonatomic,strong)CustomTabButton *buyChoiceTabBtn;

@property(nonatomic,strong)CustomTabButton *squareTabBtn;



@end

@implementation CustomTabView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.homeTabBtn];
        [self addSubview:self.buyChoiceTabBtn];
        [self addSubview:self.buyTabBtn];
        [self addSubview:self.squareTabBtn];
        [self addSubview:self.mineTabBtn];
        
        //set subviews block
        [self blockForSubViews];
        
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat btnW = (self.frame.size.width - 4) / 5;
    CGFloat btnH = self.frame.size.height - 1;
    
    [self.homeTabBtn setFrame:CGRectMake(0, 1, btnW, btnH)];
    [self.buyChoiceTabBtn setFrame:CGRectMake(btnW + 1, 1, btnW, btnH)];
    [self.buyTabBtn setFrame:CGRectMake(btnW * 2 + 2, 1, btnW, btnH)];
    [self.squareTabBtn setFrame:CGRectMake(btnW * 3 + 3, 1, btnW, btnH)];
    [self.mineTabBtn setFrame:CGRectMake(btnW * 4 + 4, 1, btnW, btnH)];
}

- (void)blockForSubViews
{
    __block CustomTabButton *buyBtn = self.buyTabBtn;
    __block CustomTabButton *mineBtn = self.mineTabBtn;
    __block CustomTabButton *homeBtn = self.homeTabBtn;
    __block CustomTabButton *buyChoiceBtn = self.buyChoiceTabBtn;
    __block CustomTabButton *squareBtn = self.squareTabBtn;
    
    self.homeTabBtn.selectedBlock = ^(CustomTabButton *btn){
        
        [btn showFromSelectedStatus:YES];
        [buyBtn showFromSelectedStatus:NO];
        [mineBtn showFromSelectedStatus:NO];
        [buyChoiceBtn showFromSelectedStatus:NO];
        [squareBtn showFromSelectedStatus:NO];
    };
    
    self.buyTabBtn.selectedBlock = ^(CustomTabButton *btn){
        
        [btn showFromSelectedStatus:YES];
        [homeBtn showFromSelectedStatus:NO];
        [mineBtn showFromSelectedStatus:NO];
        [buyChoiceBtn showFromSelectedStatus:NO];
        [squareBtn showFromSelectedStatus:NO];
    };
    
    self.mineTabBtn.selectedBlock = ^(CustomTabButton *btn){
        
        [btn showFromSelectedStatus:YES];
        [buyBtn showFromSelectedStatus:NO];
        [homeBtn showFromSelectedStatus:NO];
        [buyChoiceBtn showFromSelectedStatus:NO];
        [squareBtn showFromSelectedStatus:NO];
    };
    
    self.buyChoiceTabBtn.selectedBlock = ^(CustomTabButton *btn){
        
        [btn showFromSelectedStatus:YES];
        [buyBtn showFromSelectedStatus:NO];
        [homeBtn showFromSelectedStatus:NO];
        [mineBtn showFromSelectedStatus:NO];
        [squareBtn showFromSelectedStatus:NO];
    };
    self.squareTabBtn.selectedBlock = ^(CustomTabButton *btn){
        
        [btn showFromSelectedStatus:YES];
        [buyBtn showFromSelectedStatus:NO];
        [homeBtn showFromSelectedStatus:NO];
        [buyChoiceBtn showFromSelectedStatus:NO];
        [mineBtn showFromSelectedStatus:NO];
    };
    
}

#pragma mark --delegate Area
- (void)customTabButton:(CustomTabButton *)tabBtn
{
    [self.delegate customTableWithBarItemTag:tabBtn.tag];
}
#pragma mark --lazy load Area
- (CustomTabButton *)homeTabBtn
{
    if (_homeTabBtn == nil) {
        _homeTabBtn = [CustomTabButton customTabButton];
        [_homeTabBtn title:@"首页" image:[UIImage imageNamed:@"tab_home"] selectedImage:[UIImage imageNamed:@"tab_home_on"]];
        [_homeTabBtn showFromSelectedStatus:YES];
        _homeTabBtn.delegate = self;
        _homeTabBtn.tag = 1;//用来标识以及传递数据
    }
    return _homeTabBtn;
}

- (CustomTabButton *)buyTabBtn
{
    if (_buyTabBtn == nil) {
        _buyTabBtn = [CustomTabButton customTabButton];
        [_buyTabBtn title:@"买么" image:[UIImage imageNamed:@"tab_reward"] selectedImage:[UIImage imageNamed:@"tab_reward_on"]];
        [_buyTabBtn showFromSelectedStatus:NO];
        _buyTabBtn.delegate = self;
        _buyTabBtn.tag = 3;
    }
    return _buyTabBtn;
}

- (CustomTabButton *)mineTabBtn
{
    if (_mineTabBtn == nil) {
        _mineTabBtn = [CustomTabButton customTabButton];
        [_mineTabBtn title:@"我的" image:[UIImage imageNamed:@"tab_my"] selectedImage:[UIImage imageNamed:@"tab_my_on"]];
        [_mineTabBtn showFromSelectedStatus:NO];
        _mineTabBtn.delegate = self;
        _mineTabBtn.tag = 5;
    }
    return _mineTabBtn;
}

- (CustomTabButton *)buyChoiceTabBtn
{
    if (_buyChoiceTabBtn == nil) {
        _buyChoiceTabBtn = [CustomTabButton customTabButton];
        [_buyChoiceTabBtn title:@"值得买" image:[UIImage imageNamed:@"tab_notice"] selectedImage:[UIImage imageNamed:@"tab_notice_on"]];
        [_buyChoiceTabBtn showFromSelectedStatus:NO];
        _buyChoiceTabBtn.delegate = self;
        _buyChoiceTabBtn.tag = 2;
    }
    return _buyChoiceTabBtn;
}

- (CustomTabButton *)squareTabBtn
{
    if (_squareTabBtn == nil) {
        _squareTabBtn = [CustomTabButton customTabButton];
        [_squareTabBtn title:@"广场" image:[UIImage imageNamed:@"tab_search"] selectedImage:[UIImage imageNamed:@"tab_search_on"]];
        [_squareTabBtn showFromSelectedStatus:NO];
        _squareTabBtn.delegate = self;
        _squareTabBtn.tag = 4;
    }
    return _squareTabBtn;
}

@end
